<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Deliveryman extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		if (! $this->session->userdata ( 'logged_in' )) {
			redirect ( 'index_admin/login' );
			exit ();
		}
	}
	/*
	 * 返回送货员列表
	 */
	public function index() {
		$this->load->model ( 'deliveryman_model' );
		$deliverymans= $this->deliveryman_model->find_all_deliverymans ();
		$data ['deliverymans'] =$deliverymans;
		$data ['title'] = '送货员管理 - 员工管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'deliveryman/index' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回添加送货员页面
	 */
	public function add() {
		$data ['title'] = '添加送货员 - 员工管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'deliveryman/add' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 返回编辑送货员页面
	 */
	public function edit() {
		$id = $this->uri->segment(4);
		$this->load->model ( 'deliveryman_model' );
		$data ['deliveryman'] = $this->deliveryman_model->load ( $id );
		$data ['title'] = '编辑送货员 - 员工管理系统';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'deliveryman/edit' );
		$this->load->view ( 'footer' );
	}
	/*
	 * 保存送货员信息
	 */
	public function save() {
		$id = $this->input->post('id');
		$deliveryman = array (
				'name' => $this->input->post ( 'name' ),
				'gender' => $this->input->post ( 'gender' ),
				'phone' => $this->input->post ( 'phone' ),
				'type' => $this->input->post ( 'type' ),
				'account' => $this->input->post ( 'account' )
		);
		$this->load->model ( 'deliveryman_model' );
		if ($id) {
			if($this->deliveryman_model->update ( $id, $deliveryman )){
				return_json(TRUE,'','修改成功！'); 
			}else{
				return_json(FALSE,'','修改失败！');
			}
		} else {
			$deliverymanid=$this->deliveryman_model->create ( $deliveryman );
			if(!empty($deliverymanid)){
				return_json(TRUE,'','添加成功！'); 
			}else{
				return_json(FALSE,'','添加失败！');
			}
		}
	}
	/*
	 * 删除送货员信息
	 */
	public function delete() {
		$id = $this->input->post('id');
		$this->load->model ( 'deliveryman_model' );
		if ($this->deliveryman_model->delete ( $id )) {
			return_json(TRUE,'','删除成功！'); 
		}else{
			return_json(TRUE,'','删除失败！');
		}
	}
}